<?php

/**
 * Description of ApplicationCore
 *
 * @author harriagada
 */
require_once dirname(__FILE__) . "/../loader/Loader.php";

class ApplicationCore extends Loader {

    public static function DataBaseINIfile() {
        return dirname(__FILE__) . "/../../config/schemas/database.ini";
    }

    public static function ErrorsINIfile() {
        return dirname(__FILE__) . "/../../config/schemas/errors.ini";
    }

    public static function WebINIfile() {
        return dirname(__FILE__) . "/../../config/schemas/web.ini";
    }

    public static function ParseGenericINIfile($filename) {
        return parse_ini_file($filename, true);
    }

    public static function DBConnect($dbParams) {
        try {
            $connect = new PDO($dbParams['database']['driver'] . ":
                            dbname=" . $dbParams['database']['dbname'] . ";
                            host=" . $dbParams['database']['host'],
                            $dbParams['database']['username'],
                            $dbParams['database']['password']);
            $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $connect;
        } catch (PDOException $e) {
            echo "Error de conexión: " . $e->getMessage();
        }
    }

    public function ERRORConnect($errParams) {
        $applicationCore = new ApplicationCore();
        $errPath = dirname(__FILE__) . "/../../errors/";

        try {
            foreach ($errParams as $err) {
                $applicationCore->errorFullPaths[$err["execute"]]["in"] = $errPath . $err["execute"] . "/in/" . $err["execute"] . "IN.php";
                $applicationCore->errorFullPaths[$err["template"]]["out"] = $errPath . $err["template"] . "/out/" . $err["template"] . "OUT.php";
            }

            return $applicationCore->errorFullPaths;
        } catch (Exception $e) {
            echo "Error: " . $e->getMessage();
        }
    }

    public function WEBConnect($webParams) {
        $this->title = $webParams["resources"]["title"];
        $this->stylesheet = $webParams["resources"]["stylesheet"];
        $this->javascript = $webParams["resources"]["javascript"];
        $this->layout = $webParams["resources"]["layout"];

        $this->databaseEnabled = $webParams["database"]["enabled"];

        $this->sessionEnabled = $webParams["session"]["enabled"];

        $this->libsAutoload = $webParams["libs"]["autoload"];

        $this->validateEnabled = $webParams["validate"]["enabled"];
        $this->validateAjax = $webParams["validate"]["ajax"];

        $this->encryptionGet = $webParams["encryption"]["get"];
        $this->encryptionPost = $webParams["encryption"]["post"];

        foreach ($webParams["meta"] as $meta => $key) {
            $this->meta[$meta] = $key;
        }

        $this->LoadParamsLayout();
    }

    public function StyleSheet() {
        return $this->GenericPathResources($this->stylesheet, "css");
    }

    public function JavaScript() {
        return $this->GenericPathResources($this->javascript, "js");
    }

    public function LoadParamsLayout() {
        $styleSheetPath = $this->StyleSheet();
        $javaScriptPath = $this->JavaScript();

        if ($this->databaseEnabled == true) {
            $databaseArray = $this->ParseGenericINIfile($this->DataBaseINIfile());
            $this->databaseObject = $this->DBConnect($databaseArray);
        }

        if ($this->libsAutoload == true) {
            $this->libsPath = dirname(__FILE__) . "/../";
            Loader::IncludeAllClass($this->libsPath);

            for ($pos = 0; $pos < count($styleSheetPath); $pos++) {
                $this->HTMLTags['CSS']["css$pos"] = Helpers::LinkCSSTag($styleSheetPath[$pos]);
            }

            for ($pos = 0; $pos < count($javaScriptPath); $pos++) {
                $this->HTMLTags['JS']["js$pos"] = Helpers::ScriptJSTag($javaScriptPath[$pos]);
            }

            if ($this->meta["charset"]) {
                $this->HTMLTags["META"]["charset"] = Helpers::MetaTagCharset($this->meta["charset"]);
            }

            foreach ($this->meta as $metaTag => $key) {
                if ($metaTag != "charset") {
                    $this->HTMLTags['META']["$metaTag"] = Helpers::MetaTag($metaTag, $key);
                }
            }

            if ($this->title != false) {
                $this->HTMLTags['TITLE']['title'] = Helpers::PageTitle($this->title) . "\n";
            }

            if ($this->sessionEnabled == true) {
                Session::StartSession();
            }

            if ($this->validateEnabled == true) {
                $this->validateForms = new Validate();
            }

            if ($this->validateAjax == true) {
                $this->HTMLTags['AJAX']["jquery"] = Helpers::ScriptJSTag("/js/jquery.js");
                $this->HTMLTags['AJAX']["jquery.validate"] = Helpers::ScriptJSTag("/js/jquery.validate.js");
            }

            if ($this->encryptionGet == true) {
                $this->encryptionGetObject = new EncryptionGet();
            }

            if ($this->encryptionPost == true) {
                $this->encryptionPostObject = new EncryptionPost();
            }
        }
        
        $this->LayoutConnect();
    }
    
    public function GenericPathResources($fileResources, $type) {
        $resource = array();

        if (strpos($fileResources, ",")) {
            $rs = explode(",", $fileResources);
        }

        foreach ($rs as $rsFile) {
            if ($rs != "")
                $resource[] = "/$type/$rsFile.$type";
        }

        return $resource;
    }

}

?>
